/*
 *      physics.cpp
 *      
 *      
 * 		2009 Víctor Manuel Ochoa <kanito8a@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */


#include "physics.h"


Physics::Physics()
{
	
}

Physics::~Physics()
{
	
}

bool Physics::init()
{
	m_position=Vector2(0.f,0.f);
	m_velocity=Vector2(0.f,0.f);
	m_scale=Vector2(0.f,0.f);
	m_speed=0.f;
 	m_acceleration=0.f;
 	m_gravity=BASIC_GRAVITY;
	m_gravityaccel=0.f;
 	m_angle=0.f;
	m_angle_real=0.f;
 	m_elasticity=0.f;	
	m_mass=1.f;	
	m_inground=false;
	m_isfloor=false;
	return true;	
}

bool Physics::init(Vector2 _pos,Vector2 _veloc,float _angle,float _speed, float _acc, float _mass)
{
	m_position=_pos;
	m_velocity=_veloc;
	m_scale=Vector2(0.f,0.f);
	m_speed=_speed;
 	m_acceleration=0.f;
 	m_gravity=BASIC_GRAVITY;
	m_gravityaccel=0.f;
 	m_angle=_angle;
	m_angle_real=_angle;
 	m_elasticity=0.f;	
	m_mass=_mass;	
	m_inground=false;
	m_isfloor=false;
	return true;	
}


void Physics::freeMemory()  
{
}

void Physics::update()
{
	m_scale.x=cos(m_angle*PI_DIV_180);
	m_scale.y=sin(m_angle*PI_DIV_180);
		
	m_speed+=m_acceleration;
	
	m_velocity=(m_speed*m_scale);
	
	if(!m_inground)
	{
		m_velocity.y+=m_gravityaccel;
		m_gravityaccel+=m_gravity*m_mass;
	}
	
	m_angle_real=90-(atan2(m_velocity.x,m_velocity.y)*_180_DIV_PI);
	m_position+=m_velocity;
	
	if(m_position.y>=(HEIGHT-(m_height/2)) && !m_inground)
	{
		m_position.y=HEIGHT-(m_height/2);
		m_inground=true;
		m_angle=0;
		m_speed=0;
	}
}
